package com.ccp.dev.qbdms.controller;

import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.basic.util.UUIDUtils;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.core.util.RequestUtil;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.qbdms.util.ProjectUUIDUtils;
import com.ccp.dev.system.model.SysUser;
import com.ccp.dev.core.basic.model.SysAuditModelType;
import com.ccp.dev.core.poi.excel.reader.DataEntity;
import com.ccp.dev.core.poi.excel.reader.ExcelReader;
import com.ccp.dev.core.poi.excel.reader.FieldEntity;
import com.ccp.dev.core.poi.excel.reader.TableEntity;
import java.io.PrintWriter;
import net.sf.json.JSONObject;
import java.nio.charset.StandardCharsets;
import com.ccp.dev.qbdms.model.WQbdFalProblemvso;
import com.ccp.dev.qbdms.service.WQbdFalProblemvsoService;
import com.ccp.dev.core.util.log.SysAuditThreadLocalHolder;
import com.ccp.dev.core.basic.annotion.Action;
import java.util.Date;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.ModelMap;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.ccp.dev.core.poi.excel.Excel;
import com.ccp.dev.core.poi.excel.editor.IFontEditor;
import com.ccp.dev.core.poi.excel.style.Color;
import com.ccp.dev.core.poi.excel.style.DataFormat;
import com.ccp.dev.core.poi.excel.style.font.BoldWeight;
import com.ccp.dev.core.poi.excel.style.font.Font;
import com.ccp.dev.core.poi.excel.util.ExcelUtil;
import java.util.ArrayList;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

/**
 * 质量问题版本 信息操作处理控制器
 * 
 * @author sun
 * @date 2020-05-19 11:12:57
 */
@Controller
/**
 * 请将此名称添加到日志开关
 */
@Action(customModel = "质量问题版本")
@RequestMapping("/qbdms/wQbdFalProblemvso")
public class WQbdFalProblemvsoController extends SysBaseController{
	@Resource
	private WQbdFalProblemvsoService wQbdFalProblemvsoService;

    /**
     * 功能描述: 跳转到首页
     *
     * @return: java.lang.String
     * @auther: sun
     * @date: 2020-05-19 11:12:57
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + UrlConstants.MODULE_FUNCTION_LIST)
    @Action(description="跳转到质量问题版本首页", exectype = "页面跳转日志")
    public String viewList() {
        return "wQbdFalProblemvso/list";
    }

    /**
    * 功能描述: 附带检索条件的分页查询
    *
    * @param: [request] http请求
    * @return: java.lang.Object
    * @auther: sun
    * @date: 2020-05-19 11:12:57
    */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_LIST)
    @ResponseBody
    @Action(description="附带检索条件的分页查询质量问题版本",detail="查询数据信息<#if success>成功<#else></#if>")
    public Object list(HttpServletRequest request) {
        QueryFilter queryFilter = new QueryFilter(request, true);
        //分页数据
        List<WQbdFalProblemvso> pageDatas = wQbdFalProblemvsoService.queryList(queryFilter);
        SysAuditThreadLocalHolder.putParamerter("success",true);
        return success(pageDatas, queryFilter.getPageBean().getTotalCount());
    }


    /**
     * 功能描述: 跳转到新增页面
     *
     * @return: java.lang.String
     * @auther: sun
     * @date: 2020-05-19 11:12:57
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + UrlConstants.MODULE_FUNCTION_ADD_UPDATE)
    @Action(description="跳转到质量问题版本新增页面", exectype = "页面跳转日志")
    public String viewAdd(HttpServletRequest request,ModelMap modelMap) {
        String id = RequestUtil.getString(request,"id");
        modelMap.put("id",id);
        return "wQbdFalProblemvso/add";
    }

    /**
    * 功能描述:  数据保存通用方法
    *
    * @param: wQbdFalProblemvso 保存的对象
    * @return: java.lang.Object
    * @auther: sun
    * @date: 2020-05-19 11:12:57
    */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_SAVE)
    @ResponseBody
    @Action(description="保存质量问题版本数据",detail="<#if isAdd>新增<#else>编辑</#if>质量问题版本数据<#if success>成功<#else>失败</#if>")
    public Object save(@RequestBody WQbdFalProblemvso wQbdFalProblemvso) {
        String id = wQbdFalProblemvso.getId();
        int result;
        if (StringUtil.isEmpty(id)) {
            SysAuditThreadLocalHolder.putParamerter("isAdd",true);
            //新增操作
            //新增数据前生成随机Id
            wQbdFalProblemvso.setId(ProjectUUIDUtils.getUUIDFor32());
            wQbdFalProblemvso.setTenantId(ContextUtil.getCurrentUserTenantId());
            result = wQbdFalProblemvsoService.add(wQbdFalProblemvso);
        } else {
            SysAuditThreadLocalHolder.putParamerter("success",false);
            //修改操作
            result = wQbdFalProblemvsoService.update(wQbdFalProblemvso);
        }
        if(result > 0){
            SysAuditThreadLocalHolder.putParamerter("success",true);
        }else{
            SysAuditThreadLocalHolder.putParamerter("success",false);
        }
        return result > 0 ? getResultDataSuccess("save.success") : getResultDataError("save.failure");
    }

    /**
    * 功能描述: 跳转到详情页面
    *
    * @return: java.lang.String
    * @auther: sun
    * @date: 2020-05-19 11:12:57
    */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/particulars")
    @Action(description="跳转到质量问题版本详情页面", exectype = "页面跳转日志")
    public String viewParticulars(HttpServletRequest request,ModelMap modelMap) {
        String id = RequestUtil.getString(request,"id");
        modelMap.put("id",id);
        return "wQbdFalProblemvso/particulars";
    }

    /**
    * 功能描述: 根据主键id获取一条数据的通用查询方法
    *
    * @param: id 参数对象必须是"id"
    * @return: java.lang.Object
    * @auther: sun
    * @date: 2020-05-19 11:12:57
    */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_GET)
    @ResponseBody
    @Action(description="根据主键id查询质量问题版本的一条数据",detail="根据主键id查询质量问题版本的数据<#if success>成功<#else>失败</#if>")
    public Object get(@RequestParam("id") String id) {
        WQbdFalProblemvso wQbdFalProblemvso = wQbdFalProblemvsoService.getOneById(id);
        if (null == wQbdFalProblemvso) {
            SysAuditThreadLocalHolder.putParamerter("success",false);
            return getResultDataError("get.failure");
        }
        SysAuditThreadLocalHolder.putParamerter("success",true);
        return getResultDataSuccess("get.success",wQbdFalProblemvso);
    }

    /**
    * 功能描述: 根据主键ID删除数据的通用删除方法
    *
    * @param: id 参数名称必须是"id"
    * @return: java.lang.Object
    * @auther: sun
    * @date: 2020-05-19 11:12:57
    */
    @PostMapping(UrlConstants.MODULE_FUNCTION_DELETE)
    @ResponseBody
    @Action(description="根据主键ID删除质量问题版本数据",exectype = "删除日志",detail="删除数据信息<#if success>成功<#else>失败</#if>")
    public Object delete(@RequestParam("id") String id) {
        if (StringUtil.isEmpty(id)) {
            SysAuditThreadLocalHolder.putParamerter("success",false);
            return fail("delete.failure.message");
        }
        int result = wQbdFalProblemvsoService.delByIdArray(id.split(","));
        if(result > 0){
            SysAuditThreadLocalHolder.putParamerter("success",true);
        }else{
            SysAuditThreadLocalHolder.putParamerter("success",false);
        }
        return result > 0 ? getResultDataSuccess("delete.success") : getResultDataError("delete.failure");
    }

    /**
    * 功能描述: 跳转到导入页面
    *
    * @return: java.lang.String
    * @auther: hw
    * @date: 2019-12-25 19:21:40
    */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/import")
    @Action(description="跳转到质量问题版本导入页面", exectype = "页面跳转日志")
    public String viewImport() {
        return "wQbdFalProblemvso/import";
    }

    /**
    * 功能描述: 导出质量问题版本数据信息
    *
    * @param: [request] http请求
    * @return: java.lang.Object
    * @auther: sun
    * @date: 2020-05-19 11:12:57
    */
    @RequestMapping("/exportData")
    @Action(description="导出质量问题版本数据信息",detail="导出质量问题版本数据信息<#if success>成功<#else></#if>")
    public void export(HttpServletRequest request, HttpServletResponse response) throws Exception{
        String[] id = RequestUtil.getStringAryByStr(request,"id");
        QueryFilter queryFilter = new QueryFilter(request,false);
        if(BeanUtils.isNotEmpty(id) && id.length!=0){
            queryFilter.put("byId",true);
            queryFilter.put("id",id);
        }else{
            queryFilter.put("byId",false);
        }
        SysAuditThreadLocalHolder.putParamerter("success",true);
        Excel excel = new Excel();
        String fileName = "WQbdFalProblemvso";//导出的文件名称
        excel.sheet().sheetName("sheet");// 重命名当前处于工作状态的表的名称
        List<String> titleList = new ArrayList<>();
        titleList.add("主键");
        titleList.add("填写人");
        titleList.add("填写人的账号");
        titleList.add("填写部门");
        titleList.add("填写部门id");
        titleList.add("密级");
        titleList.add("质量问题名称");
        titleList.add("问题发生时间");
        titleList.add("型号代号");
        titleList.add("型号名称");
        titleList.add("型号批次");
        titleList.add("型号类别");
        titleList.add("工作阶段");
        titleList.add("故障产品名称");
        titleList.add("填写日期");
        titleList.add("型号研制阶段");
        titleList.add("故障产品所属的产品批次");
        titleList.add("生产分类");
        titleList.add("故障件阶段");
        titleList.add("产品级别");
        titleList.add("产品设计单位");
        titleList.add("产品生产单位");
        titleList.add("问题发生地点");
        titleList.add("所属分系统名称");
        titleList.add("是否首飞");
        titleList.add("是否靶场质量问题");
        titleList.add("是否批次性");
        titleList.add("是否三类问题");
        titleList.add("问题等级");
        titleList.add("硬件/软件");
        titleList.add("问题级别");
        titleList.add("归零周期");
        titleList.add("问题/故障现象描述");
        titleList.add("深层次原因");
        titleList.add("问题原因描述");
        titleList.add("“四个两”要求对照分析");
        titleList.add("原因分类1");
        titleList.add("原因分类2");
        titleList.add("问题责任单位");
        titleList.add("责任单位性质");
        titleList.add("是否完成归零");
        titleList.add("归零方式");
        titleList.add("解决情况");
        titleList.add("问题处理方式");
        titleList.add("纠正措施");
        titleList.add("外协外购乙方");
        titleList.add("范围");
        titleList.add("行业");
        titleList.add("外协性质");
        titleList.add("软件等级");
        titleList.add("是否变更");
        titleList.add("当前版本");
        titleList.add("是否展示");
        titleList.add("更新人");
        titleList.add("更新人部门");
        titleList.add("更新日期");
        titleList.add("变更原始id");
        titleList.add("变更上一版本id");
        titleList.add("所属单位");
        titleList.add("原因分类3");
        titleList.add("状态");
        titleList.add("督办id");
        titleList.add("管理因素分析");
        titleList.add("技术归零时间");
        titleList.add("管理归零时间");
        titleList.add("是否集团内配套");
        titleList.add("技术归零报告及相关证明文件");
        titleList.add("管理归零报告及相关证明文件");
        titleList.add("是否责任追究");
        titleList.add("责任追究类别");
        titleList.add("认定情况");
        titleList.add("是否完成责任追究");
        titleList.add("处罚总金额");
        titleList.add("版本号");
        titleList.add("版本日期");
        titleList.add("租户Id");
        titleList.add("创建人");
        titleList.add("创建时间");
        titleList.add("更新人");
        titleList.add("更新时间");
        excel.row(0, 0).value(titleList.toArray());
        excel.row(0,0).font(new IFontEditor() {
            // 设置字体
            @Override
            public void updateFont(Font font) {
                font.boldweight(BoldWeight.BOLD);// 粗体
            }
        }).bgColor(Color.GREY_25_PERCENT).width(3500);
        excel.row(0, 1).width(3500);
        excel.row(0, 2).width(3500);
        excel.row(0, 3).width(3500);
        excel.row(0, 4).width(3500);
        //设置列的数据格式
        excel.sheet().setDefaultColumnStyle(0, DataFormat.TEXT);
        excel.sheet().setDefaultColumnStyle(1, DataFormat.TEXT);
        excel.sheet().setDefaultColumnStyle(2, DataFormat.TEXT);
        excel.sheet().setDefaultColumnStyle(3, DataFormat.TEXT);
        ExcelUtil.downloadExcel(wQbdFalProblemvsoService.exportExcel(excel,queryFilter), fileName, response);
    }

    /**
    * 导入资源文件
    * @param request
    * @param response
    * @throws Exception
    */
    @RequestMapping("/importData")
    public void importData(MultipartHttpServletRequest request, HttpServletResponse response) throws Exception{
        MultipartFile multipartFile = request.getFile("file");
        PrintWriter writer = response.getWriter();
        try {
            String fileName = multipartFile.getOriginalFilename();
            String extName = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());
            //读取excel文件，并获取每行的数据
            ExcelReader excelReader = new ExcelReader();
            TableEntity excelEntity = excelReader.readFile(multipartFile.getInputStream());
            List<DataEntity> rowDataEntityList = excelEntity.getDataEntityList();

            List<WQbdFalProblemvso> listToInsert = new ArrayList<>();
            for (DataEntity dataEntity : rowDataEntityList) {
                //获取每列的数据
                List<FieldEntity> fieldEntityList = dataEntity.getFieldEntityList();
                WQbdFalProblemvso dataToInsert = new WQbdFalProblemvso();
                int i =0;
                //根据列的顺序，给相应的对象赋值
                for (FieldEntity fieldEntity : fieldEntityList) {
                    //if(i==0){
                        //dataToInsert.setName(fieldEntity.getValue());
                    //}
                    i++;
                }
                dataToInsert.setId(ProjectUUIDUtils.getUUIDFor32());
                listToInsert.add(dataToInsert);
            }
            wQbdFalProblemvsoService.addList(listToInsert);
            SysAuditThreadLocalHolder.putParamerter("success",true);
            JSONObject json = JSONObject.fromObject(getResultDataSuccess("operation.success"));
            writer.print(new String(json.toString().getBytes(StandardCharsets.UTF_8),StandardCharsets.ISO_8859_1));
        } catch (Exception e) {
            SysAuditThreadLocalHolder.putParamerter("success",false);
            JSONObject json = JSONObject.fromObject(getResultDataError("operation.failure"));
            writer.print(new String(json.toString().getBytes(StandardCharsets.UTF_8),StandardCharsets.ISO_8859_1));
        }
    }


}
